package com.panda.pandawash.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.loopj.android.http.AsyncHttpResponseHandler;
import com.panda.pandawash.R;
import com.panda.pandawash.application.CDApplication;
import com.panda.pandawash.bean.HttpResultBean;
import com.panda.pandawash.bean.StrongBoxBean;
import com.panda.pandawash.util.HttpUtil;
import com.panda.pandawash.util.WsUtils;
import com.panda.pandawash.view.SetSafeDialog;
import com.panda.pandawash.view.StatusBarCompat;

import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

/**
 * Created by gaowf on 2017/2/10.
 */

public class SafeBoxConfirmActivity extends Activity {
    private static String TAG = "API_TAG";
    @ViewInject(R.id.edit_input_psd_safe_box)
    private EditText inputSafeBoxPsd;
    private String getInputPsd;

    @ViewInject(R.id.edit_confirm_psd_safe_box)
    private EditText confirmSafeBoxPsd;
    private String getConfirmPsd;

    @ViewInject(R.id.btn_safe_box_psd_return_confirm)
    private ImageView imageView;

    @ViewInject(R.id.btn_psd_confirm_safe_box)
    private Button confirmButton;
    private Intent mIntent;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_confirm_psd_safe_box);
        x.view().inject(this);

        if(Build.VERSION.SDK_INT>= Build.VERSION_CODES.KITKAT) {
            StatusBarCompat.compat(this, getResources().getColor(R.color.colorPrimary));
        }

        mIntent = getIntent();
        initRes();

        confirmButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                getInputPsd = inputSafeBoxPsd.getText().toString();
                getConfirmPsd = confirmSafeBoxPsd.getText().toString();

                if (!WsUtils.isMatch(WsUtils.REGEX_STRONGPASSWORD,getInputPsd)) {
                    showMessage(getString(R.string.safe_box_password_legal));
                    inputSafeBoxPsd.requestFocus();
                    return;
                }

                if((!getInputPsd.equals(getInputPsd.trim()))||(!getConfirmPsd.equals(getConfirmPsd.trim()))){
                    showMessage(getString(R.string.registe_password_have_space));
                    inputSafeBoxPsd.requestFocus();
                    return;
                }
                if(!getInputPsd.trim().equals(getConfirmPsd.trim())){
                    showMessage(getString(R.string.registe_password_not_same));
                    inputSafeBoxPsd.requestFocus();
                    return;
                }

                safeBoxChangePSW();
            }
        });
    }

    @Event(R.id.btn_safe_box_psd_return_confirm)
    private void excuteReturn(View v) {
        finish();
    }

    private void initRes() {

        inputSafeBoxPsd.addTextChangedListener(watcher);
        getInputPsd = inputSafeBoxPsd.getText().toString().trim();

        confirmSafeBoxPsd.addTextChangedListener(watcher);
        getConfirmPsd = confirmSafeBoxPsd.getText().toString().trim();
        Log.d("hys123","getInputPsd "+ getInputPsd +" getConfirmPsd " + getConfirmPsd);
        //手机号码 11 位， 验证码
        if (getInputPsd.length() > 0 && getConfirmPsd.length() > 0) {
            confirmButton.setBackgroundResource(R.drawable.ic_register_btn);
            confirmButton.setTextColor(0xffffffff);
            confirmButton.setClickable(true);
        } else {
            confirmButton.setBackgroundResource(R.drawable.ic_btn_disable);
            confirmButton.setTextColor(0x80ffffff);
            confirmButton.setClickable(false);
        }
    }
    private TextWatcher watcher = new TextWatcher() {

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            // TODO Auto-generated method stub

        }

        @Override
        public void afterTextChanged(Editable s) {
            if (s.toString().length() > 0) {
                confirmButton.setBackgroundResource(R.drawable.ic_register_btn);
                confirmButton.setClickable(true);
                confirmButton.setTextColor(0xffffffff);
                getInputPsd = inputSafeBoxPsd.getText().toString().trim();
                getConfirmPsd = confirmSafeBoxPsd.getText().toString().trim();

            } else {
                confirmButton.setBackgroundResource(R.drawable.ic_btn_disable);;
                confirmButton.setClickable(false);
                confirmButton.setTextColor(0x88ffffff);
            }

        }
    };

    private void safeBoxChangePSW(){
        String account = CDApplication.getIntance().getUserInfo().getUseraccount();
        String password = inputSafeBoxPsd.getText().toString().trim();
        String idCode = mIntent.getStringExtra("idCode");
        StrongBoxBean strongBoxBean = new StrongBoxBean();
        strongBoxBean.setNewsecretpassword(password);

        HttpUtil.forceChangeStrongBoxPSWWithIdCode(new SafeBoxChangePSWCallback(),account,account,strongBoxBean,idCode);
    }

    private class SafeBoxChangePSWCallback extends AsyncHttpResponseHandler {
        @Override
        public void onSuccess(int statusCode, String content) {
            super.onSuccess(statusCode, content);
            Log.d(TAG, "SafeBoxSetupCallback statusCode = " + statusCode);
            Log.d(TAG, "SafeBoxSetupCallback onSuccess = " + content);
            if (statusCode == 200) {
                HttpResultBean resultBean = HttpUtil.parserHttpResultInfo(content, null);
                Log.d(TAG, "resultBean = " + resultBean);
                if (resultBean != null) {
                    String code = resultBean.getCode();
                    String message = resultBean.getMessage();
                    Log.d(TAG, "code = " + code);
                    if(code.equals("0")){
                        showMessage(getString(R.string.safe_box_password_setup_success));
                        Intent intent = new Intent(SafeBoxConfirmActivity.this, SafeBoxActivity.class);
                        startActivity(intent);
                        finish();
                    }else if(code.equals("99")){
                        showMessage(message);
                        //finish();
                    }
                }
            } else {
                showMessage(getString(R.string.safe_box_password_setup_fail));
            }
        }

        @Override
        public void onFailure(Throwable error, String content) {
            super.onFailure(error, content);

            Log.d(TAG, "onFailure = " + error);
            if (error.toString().contains("refused")) {

            }
        }


    }

    private void showMessage(String string) {
        Toast toast = Toast.makeText(getApplicationContext(), string, Toast.LENGTH_SHORT);
        toast.setGravity(Gravity.CENTER, 0, 0);
        toast.show();
    }
}
